package com.cfets.annualAssessment.backgroundController;

import com.cfets.annualAssessment.entity.AnnualInform;
import com.cfets.annualAssessment.entity.Honor;
import com.cfets.annualAssessment.entity.TableContainer;
import com.cfets.annualAssessment.service.AnnualInformService;
import com.cfets.annualAssessment.service.MonitorService;
import com.cfets.annualAssessment.util.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * Created by ShenYiChou on 2017/2/20.
 */
@Controller
@RequestMapping("/admin/honor")
public class HonorController {
    @Autowired
    private MonitorService monitorService;

    @Autowired
    private AnnualInformService annualInformService;

    @RequestMapping("/index")
    public String index(Model model){
        return "honor";
    }

    @RequestMapping(value = "/Get",method = RequestMethod.POST)
    public ResponseEntity<?> getHonorList(Honor honor){
        List<Honor> monitorList = monitorService.getHonorPage(honor);
        TableContainer tableContainer = new TableContainer();
        tableContainer.setAaData(monitorList);
        tableContainer.setiTotalDisplayRecords(honor.getTotalCount());
        tableContainer.setiTotalRecords(honor.getTotalCount());
        return new ResponseEntity<>(tableContainer, HttpStatus.OK);
    }

    @RequestMapping(value = "/PostHonor",method = RequestMethod.POST)
    public ResponseEntity<?> createOrUpdateHonor(@Valid Honor honor, BindingResult result) {
        if (result.hasErrors()) {
            return new ResponseEntity<>(result.getAllErrors(), HttpStatus.OK);
        }
        boolean flag = false;
        if(honor.getId()!=null && honor.getId()!=0){
            flag = this.monitorService.updateHonor(honor);
        }
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/Honor/{id}",method = RequestMethod.DELETE)
    public ResponseEntity<?> deleteHonor(@PathVariable("id") int id){
        boolean flag = this.monitorService.deleteHonorById(id);
        return new ResponseEntity<>(flag, HttpStatus.OK);
    }

    @RequestMapping(value = "/import",method = RequestMethod.POST)
    public ResponseEntity<?> importExcel(MultipartFile honorFile) {
        try {
            Workbook xwb = WorkbookFactory.create(honorFile.getInputStream());
            Sheet sheet = xwb.getSheetAt(0);
            Row row;
            for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
                if (i == 0) {
                    continue;
                }
                row = sheet.getRow(i);
                Honor honor = new Honor();
                if(Objects.equals(row.getCell(1).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                AnnualInform annualInform = new AnnualInform();
                annualInform.setId_num(row.getCell(1).toString());
                if(this.annualInformService.getAnnualInform(annualInform).isEmpty()){
                    return new ResponseEntity<>(-2, HttpStatus.OK);//证件不存在 -2
                }
                honor.setId_num(row.getCell(1).toString());

                if(Objects.equals(row.getCell(6).toString(), "")){
                    return new ResponseEntity<>(-1, HttpStatus.OK);//新增失败为空 -1
                }
                honor.setHonor(row.getCell(6).toString());
                honor.setName(row.getCell(2).toString());
                honor.setCa_num(row.getCell(3).toString());
                honor.setOrg_name(row.getCell(4).toString());
                honor.setDate(row.getCell(5).toString());
                if(this.monitorService.getHonors(honor).isEmpty()) {
                    boolean flag = this.monitorService.newHonor(honor);
                    if (!flag) {
                        return new ResponseEntity<>(false, HttpStatus.OK);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(false, HttpStatus.OK);
        }
        return new ResponseEntity<>(true, HttpStatus.OK);
    }
    @RequestMapping("/exportEmail")
    public String exportEmail(HttpServletResponse response){
        List<Honor> honorList = this.monitorService.AllHonor();
        ExcelUtil.exportExcel("表彰信息", new String[]{"NO.","姓名","证件号码","资格证书号码","机构名称","时间","表彰名称"},
                new String[]{"","name","id_num","ca_num","org_name","date","honor"}, honorList, response );
        return null;
    }

}
